var mongoose = require('mongoose');
const Cart = require('../models/cart.model');

// 新增
exports.create = function(req, res, next) {
    const cart = new Cart(req.body);
    cart.save().then((data) => {
        res.json(data);
    });
}

exports.update = function(req, res, next) {
    const id = req.params.id;
    Cart.findByIdAndUpdate(id, { $set: req.body }, { new: false })
        .then(data => {
            res.json(data); // 这里返回的数据是修改之前的数据，不是修改之后的数据
    })
}

//删除单个
exports.remove = function(req, res, next) {
    const id = req.params.id;
    Cart.findByIdAndDelete(id, (err, data) => {
        res.json(data);
    })
}

//获取数据  加上了分页
exports.list = function(req, res, next) {
    var page = req.body.page ? req.body.page : 1;
    var rows = req.body.rows ? req.body.rows : 10;

    var queryCondition = {};  //查询条件
    if (req.body.productName && req.body.productName.trim().length > 0) {
        productName    = req.body.productName;
        queryCondition = {
            "productName": new RegExp(productName, 'i')
        }
    }
    Cart.paginate(queryCondition, { page: +page, limit: +rows }, function(err, result) {
        result.rows = result.docs;
        delete result.docs;
        res.json(result);
    });

}


//根据id获取单个数据
exports.getData = function(req, res, next) {
        var id = req.params.id;
        Cart.findById(id).then(data => {
            res.json(data);
        })
    }
    //删除多个
exports.removes = function(req, res, next) {
    var ids = req.body.ids.split(',');
    if (ids.length > 0) {
        Cart.remove({ _id: { $in: ids } }).then(data => {
            res.json({
                'message': 'delete ok',
                status   : 200
            })
        })
    }
}